<template>
    <div>
        <div style=" margin:0 auto;width:80%;">
            <h3>服务器:{{ ssh_host }}</h3>
            <MyTerminal v-if="show_terminal" style="height:750px;overflow:auto;margin-top:10px"  :terminal="terminal" ref="xterm">

            </MyTerminal>
        </div>
    </div>
</template>


<script>
import axios from 'axios';
import MyTerminal from '@/lib/sshTerminal';
import { server_user_info } from  '@/api'
export default {
    components:{
        MyTerminal
    },
    data() {
        return {
            data: "",
            terminal: {
                pid: 1,
                name: "terminal",
                cols: 50,
                rows: 100,
                websocket_url: '',
            },
            ssh_host: '',

            ssh_user: '',
            ssh_password: '',
            ssh_port: '',
            auth_type: 0,
            private_key: '',

            show_terminal: false
        }
    },
    methods: {        
        async get_server_user_info(){
            try {
                let username = localStorage.getItem("username")
                let data = {
                    "username":username
                }
                let response = await axios({
                    url: server_user_info,
                    data: data,
                    method: 'post'
                })
                let serverUser = response.data
                if(serverUser=={}){
                    alert("查不到系统账号")
                }else{
                    this.ssh_user =serverUser.name
                    this.ssh_password = serverUser.password
                    this.ssh_port = serverUser.ssh_port
                    this.auth_type = serverUser.auth_type
                    this.private_key = serverUser.private_key
                }

            } catch (error) {
                console.log(error)
                return null;
            }
        },
        randomNum(Min,Max)
        {
            var Range = Max - Min;
            var Rand = Math.random();
            return(Min + Math.round(Rand * Range));
        },
        async refresh(){

            // this.$refs.xterm.parentRefresh()  因为this.show_terminal不是true所以获取不到xterm的
            let query = this.$router.currentRoute.query
            this.ssh_host = query.hostname
            await this.get_server_user_info()
            let  webssh_server_addr  = process.env.VUE_APP_WEBSSH_URL
            let rnumber = this.randomNum(1,1000000)
            console.log(rnumber)
            this.terminal.pid=rnumber
            this.terminal.websocket_url = webssh_server_addr+'?hostname='+this.ssh_host+'&port='+this.ssh_port+'&username='+this.ssh_user+'&password='+this.ssh_password+"&id="+rnumber
            this.show_terminal = true
        }
    },
    mounted: function() {
        this.refresh()
    },


}
        
</script>